/*jslint browser: true, cap: false, glovar: true, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

SONGHAY.ui.TablePageViewer =  {

    pageBack: function(vTableId)
    {
        var vCaptionId = (arguments.length > 1) ? arguments[1] : null;
        this.pageTable(vTableId,vCaptionId,false);
    },

    pageForward: function(vTableId)
    {
        var vCaptionId = (arguments.length > 1) ? arguments[1] : null;
        this.pageTable(vTableId,vCaptionId,true);
    },

    pageTable: function(vTableId)
    {
        var oTable = window.document.getElementById(vTableId);
        if(oTable && oTable.tBodies[0]){} else { return; }

        var oCaption = (arguments.length > 1) ?
            window.document.getElementById(arguments[1]) : null;
        var vPageForward = (arguments.length > 2) ? arguments[2] : null;

        var vPage = oTable.rasxCurrentPage ? parseInt(oTable.rasxCurrentPage,10) : 0;
        var vPageCount = oTable.rasxPageCount ? parseInt(oTable.rasxPageCount,10) : 0;

        if(vPageForward === null)
        {
            //Add custom properties to table element:
            if(oTable.rasxCurrentPage){}
            else { oTable.rasxCurrentPage = vPage; }
            if(oTable.rasxPageCount){}
            else
            { 
                oTable.rasxPageCount = oTable.tBodies.length;
                vPageCount = parseInt(oTable.rasxPageCount,10);
            }
        }
        else
        {
            if(vPageForward) { vPage++; }
            else { vPage--; }
        }

        //Keep page number within limits:
        if(vPage < 0) { vPage = 0; }
        if(vPage >= vPageCount) { vPage = vPageCount - 1; }

        //Save current page:
        if(oTable.rasxCurrentPage !== undefined) { oTable.rasxCurrentPage = vPage; }

        if(window.document.all) //Probably MSIE-like:
        {
            for(var i = 0; i < vPageCount; i++)
            {
                oTable.tBodies[i].style.display = (i == vPage) ? 'block' : 'none';
            }
        }
        else //Probably Gecko-like:
        {
            for(i = 0; i < vPageCount; i++)
            {
                oTable.tBodies[i].style.display = (i == vPage) ? 'table-row-group' : 'none';
            }
        }

        if(oCaption)
        {
            vPage++;
            oCaption.innerHTML = vPage + '/' + vPageCount;
        }
    },

    setTable: function(vTableId,vPageSize)
    {
        var oTable = window.document.getElementById(vTableId);
        if(oTable){} else { return; }

        oTable.rasxPageSize = vPageSize;
    }
};
